package com.organization.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ejb.Stateless;

import com.common.bo.Node;
import com.common.util.ObjectUtils;
import com.common.util.ObjectXmlUtils;
import com.common.util.impl.ObjectXmlUtilsAttributeImpl;
import com.core.service.AppBaseService;
import com.dpmc.entity.TB_SHGL_LXDY;
import com.dpmc.entity.TB_SHGL_LXDY_KZSX;
import com.dpmc.entity.TB_SPGL_SPXX;
import com.dpmc.entity.TB_SPGL_SPXX_SXZ;
import com.organization.entity.*;

/**
 *  
 */
@Stateless
public class SVC_ORG extends AppBaseService implements SVC_ORGRemote,
		SVC_ORGLocal {

	@Override
	public Map ADD_PARTTYPE(Map param) {
		TB_ORG_PARTY_TYPE tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PARTY_TYPE.class);
		tdo.setID(tdo.uuid());
		tdo.setCREATE_TIME(getOpTime(param));
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param));
		getJpaDao().save(tdo);
		return ObjectUtils.javaBeanToMap(tdo);
	}

	@Override
	public Map UPD_PARTTYPE(Map param) {
		TB_ORG_PARTY_TYPE tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PARTY_TYPE.class);
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param));

		TB_ORG_PARTY_TYPE po = getJpaDao().find(TB_ORG_PARTY_TYPE.class,
				tdo.getID());

		ObjectUtils.copyJavaBeanToBean(tdo, po, "CREATE_TIME");
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	
	@Override
	public void DEL_PARTTYPE(Map param) {
		TB_ORG_PARTY_TYPE tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PARTY_TYPE.class);
		StringBuffer hql = new StringBuffer(
				"select count(o) from TB_ORG_PARTY_STRUCTURE_TYPE o where PARENT_PARTY_TYPE_ID=")
				.append("'").append(tdo.getID()).append("'");
		if (queryForResultSize(hql.toString()) > 0) {
			throw new RuntimeException("存在未删除的关联！");
		}
		hql = new StringBuffer(
				"select count(o) from TB_ORG_PARTY_STRUCTURE_TYPE o where CHILD_PARTY_TYPE_ID=")
				.append("'").append(tdo.getID()).append("'");
		if (queryForResultSize(hql.toString()) > 0) {
			throw new RuntimeException("存在未删除的关联！");
		}
		getJpaDao().delete(TB_ORG_PARTY_TYPE.class, tdo.getID());
	}

	@Override
	public Map ADD_PARTSTRUCTURETYPE(Map param) {
		TB_ORG_PARTY_STRUCTURE_TYPE tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PARTY_STRUCTURE_TYPE.class);
		tdo.setID(tdo.uuid());
		tdo.setCREATE_TIME(getOpTime(param));
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param));
		getJpaDao().save(tdo);
		return ObjectUtils.javaBeanToMap(tdo);
	}

	@Override
	public Map UPD_PARTSTRUCTURETYPE(Map param) {
		TB_ORG_PARTY_STRUCTURE_TYPE tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PARTY_STRUCTURE_TYPE.class);
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param));

		TB_ORG_PARTY_STRUCTURE_TYPE po = getJpaDao().find(
				TB_ORG_PARTY_STRUCTURE_TYPE.class, tdo.getID());

		ObjectUtils.copyJavaBeanToBean(tdo, po, "CREATE_TIME");
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_PARTSTRUCTURETYPE(Map param) {
		TB_ORG_PARTY_STRUCTURE_TYPE tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PARTY_STRUCTURE_TYPE.class);

		getJpaDao().delete(TB_ORG_PARTY_STRUCTURE_TYPE.class, tdo.getID());
	}

	@Override
	public Map ADD_ORGNIZATION(Map param) {
		TB_ORG_ORGNIZATION tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_ORGNIZATION.class);

		tdo.setID(tdo.uuid());
		tdo.setCREATE_TIME(getOpTime(param));
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param));
		getJpaDao().save(tdo);

		TB_ORG_PARTY_STRUCTURE structure = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PARTY_STRUCTURE.class);
		if (structure.getPARTY_STRUCTURE_TYPE_ID() != null) {
			structure.setCHILD_ID(tdo.getID());
			structure.setCHILD_TYPE_ID("TB_ORG_ORGNIZATION");

			ADD_STRUCTURE(ObjectUtils.javaBeanToMap(structure));
		}

		return ObjectUtils.javaBeanToMap(tdo);
	}

	@Override
	public Map UPD_ORGNIZATION(Map param) {
		TB_ORG_ORGNIZATION tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_ORGNIZATION.class);
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param));

		TB_ORG_ORGNIZATION po = getJpaDao().find(TB_ORG_ORGNIZATION.class,
				tdo.getID());

		ObjectUtils.copyJavaBeanToBean(tdo, po, "CREATE_TIME");
		getJpaDao().update(po);

		HashMap in_param = new HashMap();
		StringBuffer hql = new StringBuffer(
				"delete o  from TB_ORG_PARTY_STRUCTURE o ,TB_ORG_PARTY_STRUCTURE_TYPE t where CHILD_ID=:CHILD_ID ")
				.append(" and CHILD_TYPE_ID=:CHILD_TYPE_ID")
				.append(" and t.ID = o.PARTY_STRUCTURE_TYPE_ID")
				.append(" and VIEW_CODE=:VIEW_CODE");
		in_param.put("CHILD_ID", po.getID());
		in_param.put("CHILD_TYPE_ID", "TB_ORG_ORGNIZATION");
		in_param.put("VIEW_CODE", param.get("VIEW_CODE"));
		doExecuteUpdate(hql.toString(), param);

		TB_ORG_PARTY_STRUCTURE structure = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PARTY_STRUCTURE.class);
		if (structure.getPARTY_STRUCTURE_TYPE_ID() != null) {
			structure.setCHILD_ID(tdo.getID());
			structure.setCHILD_TYPE_ID("TB_ORG_ORGNIZATION");

			ADD_STRUCTURE(ObjectUtils.javaBeanToMap(structure));
		}

		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_ORGNIZATION(Map param) {
		TB_ORG_ORGNIZATION tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_ORGNIZATION.class);

		getJpaDao().delete(TB_ORG_ORGNIZATION.class, tdo.getID());
	}
 
	public Map getOrgTree1(Map param) { 
		String opid = getOpid(param);
		String opInfoType = getOpInfoType(param);
		Map orgAcl = null;
		if (opInfoType != null && !opInfoType.isEmpty()) {
			orgAcl = new HashMap();
			if (opInfoType.equals("TB_ORG_PERSON")) {
				HashMap mParam = new HashMap();
				mParam.put("RESOURCE_TYPE", "org_data");
				copySysParam(param, mParam);
				List<Map> orgs = (List<Map>) getServiceInterface().doService(
						"ACLServiceBean", "getACL", mParam);
				for (Map m : orgs) {
					orgAcl.put(m.get("RESOURCE_ID"), m.get("RESOURCE_ID"));
				}
			} else {
				return null;
			}
		}

		Map root = null;
		List result = getResultList("Q_ORG.GET_ORG_TREE_ROOT", param);
		if (result != null && result.size() > 0) {
			root = new HashMap();
			if (orgAcl != null) {
				for (int i = result.size(); i > 0; i--) {
					Map m = (Map) result.get(i - 1);
					if (orgAcl.get(m.get("ID")) == null) {
						result.remove(i-1);
					}
				}
			}
			root.put("children", result);
			Iterator iterator = result.iterator();
			while (iterator.hasNext()) {
				Map node = (Map) iterator.next();
				HashMap c_param = new HashMap();
				c_param.put("VIEW_CODE", param.get("VIEW_CODE"));
				c_param.put("PARENT_ID", node.get("ID"));
				c_param.put("PARENT_TYPE_ID", node.get("ENTITY_NAME"));
				c_param.put("ENTITY_NAME", node.get("ENTITY_NAME"));
				node.put("children", getOrgChildren(c_param, orgAcl));
			}
		}

		return root;
	}

	private List getOrgChildren(HashMap param, Map orgAcl) {
		List result = getResultList("Q_ORG.GET_ORG_TREE_CHILDREN", param);
		if (result != null && result.size() > 0) {
			if (orgAcl != null) {
				for (int i = result.size(); i > 0; i--) {
					Map m = (Map) result.get(i - 1);
					if (orgAcl.get(m.get("ID")) == null) {
						result.remove(i-1);
					}
				}
			}
			Iterator iterator = result.iterator();
			while (iterator.hasNext()) {
				Map node = (Map) iterator.next();
				HashMap c_param = new HashMap();
				c_param.put("VIEW_CODE", param.get("VIEW_CODE"));
				c_param.put("PARENT_ID", node.get("ID"));
				c_param.put("PARENT_TYPE_ID", node.get("ENTITY_NAME"));
				c_param.put("ENTITY_NAME", node.get("ENTITY_NAME"));
				node.put("children", getOrgChildren(c_param, orgAcl));
			}
		} else {
			return null;
		}
		return result;
	}

	@Override
	public Map ADD_PERSON(Map param) {
		TB_ORG_PERSON tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PERSON.class);

		String typeID = (String) querySingleResult("select ID from TB_ORG_PARTY_TYPE where ENTITY_NAME='TB_ORG_PERSON'");
		tdo.setPART_TYPE_ID(typeID);
		tdo.setID(tdo.uuid());
		tdo.setCREATE_TIME(getOpTime(param));
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param));
		getJpaDao().save(tdo);

		Map USER = (Map) param.get("OBJECT_SYS_USER");
		if (USER != null) {
			copySysParam(param, USER);
			USER.put("INFO_ID", tdo.getID());
			USER.put("INFO_TYPE", "TB_ORG_PERSON");
			USER.put("INFO_NAME", tdo.getNAME());

			getServiceInterface()
					.doService("RMServiceBean", "addSysUser", USER);
		}

		return ObjectUtils.javaBeanToMap(tdo);
	}

	@Override
	public Map UPD_PERSON(Map param) {

		TB_ORG_PERSON tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PERSON.class);
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param));

		TB_ORG_PERSON po = getJpaDao().find(TB_ORG_PERSON.class, tdo.getID());

		ObjectUtils.copyJavaBeanToBean(tdo, po, "CREATE_TIME");
		getJpaDao().update(po);

		Map USER = (Map) param.get("OBJECT_SYS_USER");

		if (USER != null) {
			copySysParam(param, USER);
			USER.put("INFO_ID", tdo.getID());
			USER.put("INFO_TYPE", "TB_ORG_PERSON");
			USER.put("INFO_NAME", tdo.getNAME());
			if (USER.get("ID") != null) {
				getServiceInterface().doService("RMServiceBean",
						"updateSysUser", USER);
			} else {
				getServiceInterface().doService("RMServiceBean", "addSysUser",
						USER);
			}
		}
		return ObjectUtils.javaBeanToMap(po);
	}
	
	@Override
	public void UPDATE_SKILL(Map param){
		TB_ORG_SKILL tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_SKILL.class);
		if(param.get("SKILL_NAME").equals("")||param.get("SKILL_NAME")==null){
			throw new RuntimeException("技能名称不能为空！");
		}
		TB_ORG_SKILL po =getJpaDao().find(TB_ORG_SKILL.class,tdo.getID());  
 			    po.setID(tdo.getID());
 			    po.setSKILL_NAME(tdo.getSKILL_NAME());
 			    po.setSKILL_DETAIL(tdo.getSKILL_DETAIL());
			    getJpaDao().update(po);
	}
	
	@Override
	public void UPDATE_PERSON_SKILLS(Map param){
		TB_ORG_PERSON_SKILL tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PERSON_SKILL.class);
		if(10<tdo.getSKILL_MARK()||tdo.getSKILL_MARK()<1){
			throw new RuntimeException("人员技能评分应该不小于1且不大于10！你的输入是："+tdo.getSKILL_MARK());
		}else{
		StringBuffer hql = new StringBuffer("SELECT o FROM TB_ORG_PERSON_SKILL o ");
		hql.append(" WHERE o.SKILL_ID=:SKILL_ID AND o.PERSON_ID=:PERSON_ID");  
		List<TB_ORG_PERSON_SKILL> result=(List<TB_ORG_PERSON_SKILL>)getResultList(hql.toString(), param);
		if(result.size()<0)
		{
			throw new RuntimeException("未找到相关条目！");
		}
		else if(result.size()>1)
		{
			throw new RuntimeException("在数据库中找到两条相关条目，产生冲突！");
		}
		for (TB_ORG_PERSON_SKILL item : result) {
			TB_ORG_PERSON_SKILL po =getJpaDao().find(TB_ORG_PERSON_SKILL.class,item.getID());  
			    po.setPERSON_ID(tdo.getPERSON_ID());
			    po.setID(item.getID());
			    po.setSKILL_ID(tdo.getSKILL_ID());
			    po.setSKILL_MARK(tdo.getSKILL_MARK());
		    getJpaDao().update(po);
		}		
		}
	}
	@Override
	public void ADD_SKILLS(Map param){
		TB_ORG_SKILL skill_Add=ObjectUtils.mapToJavaBean(param,TB_ORG_SKILL.class);		
		String SKILL_NAME = (String) param.get("SKILL_NAME");
		String SKILL_DETAIL = (String) param.get("SKILL_DETAIL");
		skill_Add.setID(skill_Add.uuid());
		skill_Add.setSKILL_NAME(SKILL_NAME);
		skill_Add.setSKILL_DETAIL(SKILL_DETAIL);
		getJpaDao().save(skill_Add);	
	}
	@Override
	public void DEL_SKILLS(Map param){		
		if(queryForResultSize("Q_ORG_PERSON_ALLSKILL.queryForResultSize", param)>0)
		{
			throw new RuntimeException("该技能条目正在被一些技能人员使用，暂时不能删除！");
		}		
		StringBuffer  hql = new StringBuffer("delete from TB_ORG_SKILL where ID=:SKILL_ID");
		doExecuteUpdate(hql.toString(), param);					
	}	
	@Override
	public void DEL_SKILL_TO_PERSON(Map param){
		TB_ORG_PERSON_SKILL tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PERSON_SKILL.class);
		HashMap in_param = new HashMap();
		StringBuffer hql = new StringBuffer(
				"delete from TB_ORG_PERSON_SKILL where PERSON_ID=:PERSON_ID ")
				.append(" and SKILL_ID=:SKILL_ID");
		in_param.put("PERSON_ID", "PERSON_ID");
		in_param.put("SKILL_ID", "SKILL_ID");
		doExecuteUpdate(hql.toString(), param);		
	}
	
	@Override
	public void DEL_PERSON(Map param) {
		TB_ORG_PERSON tdo = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PERSON.class);
		TB_ORG_PERSON po = getJpaDao().find(TB_ORG_PERSON.class, tdo.getID());

		HashMap in_param = new HashMap();
		StringBuffer hql = new StringBuffer(
				"delete from TB_ORG_PARTY_STRUCTURE where PARENT_ID=:PARENT_ID ")
				.append(" and PARENT_TYPE_ID=:PARENT_TYPE_ID");
		in_param.put("PARENT_ID", po.getID());
		in_param.put("PARENT_TYPE_ID", "TB_ORG_PERSON");
		doExecuteUpdate(hql.toString(), param);

		hql = new StringBuffer(
				"delete from TB_ORG_PARTY_STRUCTURE where CHILD_ID=:CHILD_ID ")
				.append(" and CHILD_TYPE_ID=:CHILD_TYPE_ID");
		in_param.put("CHILD_ID", po.getID());
		in_param.put("CHILD_TYPE_ID", "TB_ORG_PERSON");
		doExecuteUpdate(hql.toString(), param);

		getJpaDao().delete(TB_ORG_ORGNIZATION.class, tdo.getID());
	}

	
	
	
	@Override
	public Map ADD_STRUCTURE(Map param) {
		TB_ORG_PARTY_STRUCTURE structure = ObjectUtils.mapToJavaBean(param,
				TB_ORG_PARTY_STRUCTURE.class);
		if (structure.getPARTY_STRUCTURE_TYPE_ID() != null) {
			StringBuffer hql = new StringBuffer(
					"SELECT o FROM TB_ORG_PARTY_STRUCTURE o WHERE CHILD_ID=:PARENT_ID and CHILD_TYPE_ID=:PARENT_TYPE_ID");

			List<TB_ORG_PARTY_STRUCTURE> list = (List<TB_ORG_PARTY_STRUCTURE>) getResultList(
					hql.toString(), param);
			StringBuffer path = new StringBuffer();
			path.append(structure.getPARENT_ID());
			for (TB_ORG_PARTY_STRUCTURE item : list) {
				path.append(",").append(item.getPATH()).append(",")
						.append(item.getCHILD_ID());
			}
			String[] pns = path.toString().split(",");
			HashSet<String> set = new HashSet<String>();
			StringBuffer tmp_path = new StringBuffer();
			for (String item : pns) {
				if (!item.isEmpty() && !set.contains(item)) {
					set.add(item);
					tmp_path.append(",").append(item);
				}
			}

			structure.setPATH(tmp_path.toString());
			structure.setID(structure.uuid());
			getJpaDao().save(structure);
		}
		return ObjectUtils.javaBeanToMap(structure);
	}

	@Override
	public void DEL_STRUCTURE(Map param) {

	}
	@Override
	public void ADD_SKILL_TO_PERSON(Map param){
		TB_ORG_PERSON_SKILL person_Skill_add=ObjectUtils.mapToJavaBean(param,TB_ORG_PERSON_SKILL.class);
		
		String person_ID = (String) param.get("PERSON_ID");
		String SKILL_ID = (String) param.get("SKILL_ID");
		person_Skill_add.setID(person_Skill_add.uuid());
		person_Skill_add.setPERSON_ID(person_ID);
		person_Skill_add.setSKILL_ID(SKILL_ID);
		float i=1;
		person_Skill_add.setSKILL_MARK(i);
		getJpaDao().save(person_Skill_add);
	}
	@Override
	public void UPDATESKILLINPERSONTABLE() {
		List result = getResultList("Q_ORG_PERSON.queryForResult");
		if (result != null && result.size() > 0) {			
			Iterator iterator = result.iterator();
			while (iterator.hasNext()) {
				TB_ORG_PERSON node = (TB_ORG_PERSON) iterator.next();
				HashMap c_param = new HashMap();
				c_param.put("PERSON_ID", node.getID());
				List result2 = getResultList("Q_ORG_PERSON_ALLSKILL.queryForResult",c_param);
				String skills="";
				Iterator iterator2 = result2.iterator();
				int i=1;
				while (iterator2.hasNext()) {
					Map node2 = (Map) iterator2.next();
					if(i!=1)skills+="\n";
					skills+=i+"、";
					skills+= node2.get("SKILL_NAME");
					skills+="…………技能评分："+node2.get("SKILL_MARK")+" [满分：10]";
					i++;
				}
/*                   更新Person表skills信息*/
			//	Map param =new HashMap();
			//	param.put("SKILLS",skills);
			//	param.put("ID",node.getID());
			//	TB_ORG_PERSON dto =  ObjectUtils.mapToJavaBean(param, TB_ORG_PERSON.class); 
				TB_ORG_PERSON po =getJpaDao().find(TB_ORG_PERSON.class, node.getID());  
				 
			 //   ObjectUtils.copyJavaBeanToBean(dto, po, "SKILLS");
 			    po.setSKILLS(skills);
 			    po.setID(node.getID());
 			    po.setCODE(node.getCODE());
 			    po.setNAME(node.getNAME());
 			    po.setGENDER(node.getGENDER());
 			    po.setBIRTHDAY(node.getBIRTHDAY());
 			    po.setMOBILE_PHONE(node.getMOBILE_PHONE());
 			    po.setEMAIL(node.getEMAIL());
 			    po.setPROFESSIONAL(node.getPROFESSIONAL());
 			    po.setQQ(node.getQQ());
 			    po.setCREATE_TIME(node.getCREATE_TIME());
 			    po.setOPID(node.getOPID());
 			    po.setOP_ITEM(node.getOP_ITEM());
 			    po.setSTATUS(node.getSTATUS());
 			    po.setPART_TYPE_ID(node.getPART_TYPE_ID());
			    getJpaDao().update(po); 			    
			}
		} 
	}
	
	
	@Override
	public void DEL_PART(Map param) {
		String ID = (String) param.get("ID");
		String ENTITY_NAME = (String) param.get("ENTITY_NAME");
		String PID=(String) param.get("PID");
		HashMap in_param = new HashMap(); 		
		StringBuffer hql = new StringBuffer("SELECT count(o) FROM TB_ORG_PARTY_STRUCTURE o ,TB_ORG_PARTY_STRUCTURE_TYPE t");
 
		hql.append(" WHERE o.PARTY_STRUCTURE_TYPE_ID=t.ID and o.PARENT_ID=:ID and t.VIEW_CODE=:VIEW_CODE");  
		if(queryForResultSize(hql.toString(), param)>0)
		{			
			throw new RuntimeException( "该目录下有其它数据，请把该目录下的数据删除后再执行此步骤"); 
 
		}
		hql = new StringBuffer(
				"delete from TB_ORG_PARTY_STRUCTURE where CHILD_ID=:CHILD_ID ")
				.append(" and CHILD_TYPE_ID=:CHILD_TYPE_ID").append(" and PARENT_ID=:PID");
		in_param.put("CHILD_ID", ID);
		in_param.put("CHILD_TYPE_ID", ENTITY_NAME);
		in_param.put("PID", PID);
		doExecuteUpdate(hql.toString(), in_param);		
		if ("TB_ORG_ORGNIZATION".equals(ENTITY_NAME)) {
			DEL_ORGNIZATION(param);
		}
	}

	@Override
	public Set getBelongToOrgs(String opINFO_ID) {
		HashSet whosIDset = new HashSet();
		if (opINFO_ID == null || opINFO_ID.isEmpty())
			return whosIDset;
		HashMap params = new HashMap();
		params.put("CHILD_ID", opINFO_ID);
		List<Map> tmpList = (List<Map>) getServiceInterface().doService(
				"SVC_ORG", "getResultList",
				"Q_ORG.GET_STRUCTURE_PATH_BY_CHILD_ID", params);
		String whosIds = "";
		for (Map item : tmpList) {
			whosIds = whosIds + item.get("PATH") + ",";
		}
		String[] ids = whosIds.split(",");

		for (String id : ids) {
			whosIDset.add(id);
		}

		return whosIDset;
	}
	@Override
	public String getOrgCheckTree(Map param){
		String opid = getOpid(param);
		String opInfoType = getOpInfoType(param);
		String WHOW_ID = (String) param.get("WHOW_ID");
		String str_ENTITY_NAME = (String) param.get("ENTITY_NAMEs");
		//查询的对象类型
		HashSet<String> setOrgType = new HashSet<String> ();
		if(str_ENTITY_NAME!=null){ 
			 String[] arrTemp = str_ENTITY_NAME.split(",");
			 for(String str : arrTemp){
				 if(str!=null && !str.isEmpty()){
					 setOrgType.add(str);
				 }
			 } 
		} 
		//可访问的组织机构
		HashSet orgAcl = null;
		//选中的组织机构
		HashSet orgChecks = new HashSet();
		if (opInfoType != null && !opInfoType.isEmpty()) {
			orgAcl = new HashSet();
			if (opInfoType.equals("TB_ORG_PERSON")) {
				HashMap mParam = new HashMap();
				mParam.put("RESOURCE_TYPE", "org_data");
				copySysParam(param, mParam);
				List<Map> orgs = (List<Map>) getServiceInterface().doService(
						"ACLServiceBean", "getACL", mParam);
	 
				for (Map m : orgs) {
					orgAcl.add(m.get("RESOURCE_ID") );
				}
			} else {
				return null;
			}
		} 
		
		if(WHOW_ID!=null){
			List<Map> orgs = (List<Map>) getServiceInterface().doService(
					"ACLServiceBean", "getACL", null,WHOW_ID,null);
			for (Map m : orgs) {
				orgChecks.add(m.get("RESOURCE_ID") );
			} 
		}  
		//机构 
		StringBuffer hql =null;
		HashMap tmp = null;
		HashMap<String, Node > tmpCache_jg  = new HashMap<String, Node > (); 
		Node root = new Node();
		root.setChildren(new ArrayList<Node>());
		for(String tb_name :setOrgType){
			hql = new StringBuffer("SELECT o FROM ").append(tb_name).append(" o WHERE STATUS=0");
			tmp = new HashMap(); 
			if(orgAcl!=null){
				hql.append(" AND o.ID IN(:ids) "); 
				tmp.put("ids", orgAcl);
			}   
			if("TB_ORG_ORGNIZATION".equals(tb_name)){
				List<TB_ORG_ORGNIZATION> list_jg = (List<TB_ORG_ORGNIZATION>) getResultList(hql.toString(),tmp);
				for(TB_ORG_ORGNIZATION item : list_jg){ 
					Node node = new Node(item.getID(),item.getNAME(),tb_name);
					if(orgChecks.contains(item.getID())){
						node.setState("1");
					}else{
						node.setState("0");
					}
					tmpCache_jg.put(item.getID(), node);
					root.getChildren().add(node);
				}
			}else if("TB_ORG_PERSON".equals(tb_name)){
				List<TB_ORG_PERSON> list_jg = (List<TB_ORG_PERSON>) getResultList(hql.toString(),tmp);
				for(TB_ORG_PERSON item : list_jg){ 
					Node node = new Node(item.getID(),item.getNAME(),tb_name);
					if(orgChecks.contains(item.getID())){
						node.setState("1");
					}else{
						node.setState("0");
					}
					tmpCache_jg.put(item.getID(), node);
					root.getChildren().add(node);
				}
			}
			
		}
		//关系
		hql = new StringBuffer("SELECT DISTINCT o FROM TB_ORG_PARTY_STRUCTURE o,TB_ORG_PARTY_STRUCTURE_TYPE t")
				.append(" WHERE o.PARTY_STRUCTURE_TYPE_ID=t.ID ")
				.append(" AND t.VIEW_CODE=:VIEW_CODE"); 
		tmp = new HashMap();
		tmp.put("VIEW_CODE", param.get("VIEW_CODE")); 
		if(orgAcl!=null){
			hql.append(" AND (o.CHILD_ID IN(:id1s) OR o.PARENT_ID IN (:id2s))"); 
			tmp.put("id1s", orgAcl);
			tmp.put("id2s", orgAcl);
		} 
		List<TB_ORG_PARTY_STRUCTURE> list_gx = (List<TB_ORG_PARTY_STRUCTURE>) getResultList(hql.toString(),tmp);
		for(TB_ORG_PARTY_STRUCTURE item : list_gx){
			Node node_c = tmpCache_jg.get(item.getCHILD_ID());
			Node node_p = tmpCache_jg.get(item.getPARENT_ID());
			if(node_c!=null && node_p!=null){
				root.getChildren().remove(node_c);
				if(node_p.getChildren()==null){
					node_p.setChildren(new ArrayList<Node>());
				}
				node_p.getChildren().add(node_c);
			}
		} 
		
		ObjectXmlUtils utils=new ObjectXmlUtilsAttributeImpl();
		return utils.objectToXml(root); 
	} 
	@Override
	public Map getOrgTree(Map param) { 
		
		String opid = getOpid(param);
		String opInfoType = getOpInfoType(param); 
		String str_ENTITY_NAME = (String) param.get("ENTITY_NAMEs");
		//查询的对象类型
		HashSet<String> setOrgType = new HashSet<String> ();
		if(str_ENTITY_NAME!=null){ 
			 String[] arrTemp = str_ENTITY_NAME.split(",");
			 for(String str : arrTemp){
				 if(str!=null && !str.isEmpty()){
					 setOrgType.add(str);
				 }
			 } 
		} 
		//可访问的组织机构
		HashSet orgAcl = null; 
		if (opInfoType != null && !opInfoType.isEmpty()) {
			orgAcl = new HashSet();
			if (opInfoType.equals("TB_ORG_PERSON")) {
				HashMap mParam = new HashMap();
				mParam.put("RESOURCE_TYPE", "org_data");
				copySysParam(param, mParam);
				List<Map> orgs = (List<Map>) getServiceInterface().doService(
						"ACLServiceBean", "getACL", mParam);
	 
				for (Map m : orgs) {
					orgAcl.add(m.get("RESOURCE_ID") );
				}
			} else {
				return null;
			}
		}  
		 
		//机构 
		StringBuffer hql =null;
		HashMap tmp = null;
		HashMap<String, Map<String, ArrayList > > tmpCache_jg  = new HashMap<String, Map<String, ArrayList > > (); 
		HashMap<String, ArrayList > root = new HashMap<String, ArrayList >();
		root.put("children", new ArrayList<HashMap>() );
	 
		int i=0;
		for(String tb_name :setOrgType){
			hql = new StringBuffer("SELECT o FROM ").append(tb_name).append(" o WHERE STATUS=0");
			tmp = new HashMap(); 
			if(orgAcl!=null){
				hql.append(" AND o.ID IN(:ids) "); 
				tmp.put("ids", orgAcl);
			}    
			
			if("TB_ORG_ORGNIZATION".equals(tb_name)){
				
				List<TB_ORG_ORGNIZATION> list_jg = (List<TB_ORG_ORGNIZATION>) getResultList(hql.toString(),tmp);
				for(TB_ORG_ORGNIZATION item : list_jg){ 
					Map node =  (Map) ObjectUtils.javaBeanToMap(item); 
					node.put("ENTITY_NAME", tb_name);
					tmpCache_jg.put(item.getID(), node);
					root.get("children").add(node);	
//					System.out.println("\n服务器端TB_ORG_ORGNIZATIONMap：");
//					System.out.println(node);
				}
				
			}else if("TB_ORG_PERSON".equals(tb_name)){

				List<TB_ORG_PERSON> list_jg = (List<TB_ORG_PERSON>) getResultList(hql.toString(),tmp);
				for(TB_ORG_PERSON item : list_jg){		
					Map node =  (Map) ObjectUtils.javaBeanToMap(item); 
					node.put("ENTITY_NAME", tb_name);
					tmpCache_jg.put(item.getID(), node);
					 	
//					System.out.println("\n服务器端TB_ORG_PERSONMap：");
//					System.out.println(node);
				}
			}
			
		}
//		System.out.println("\n服务器端root  1：");
//		System.out.println(root);
		
		
		//关系
		hql = new StringBuffer("SELECT DISTINCT o FROM TB_ORG_PARTY_STRUCTURE o,TB_ORG_PARTY_STRUCTURE_TYPE t")
				.append(" WHERE o.PARTY_STRUCTURE_TYPE_ID=t.ID ")
				.append(" AND t.VIEW_CODE=:VIEW_CODE"); 
		tmp = new HashMap();
		tmp.put("VIEW_CODE", param.get("VIEW_CODE")); 
		if(orgAcl!=null){
			hql.append(" AND (o.CHILD_ID IN(:id1s) OR o.PARENT_ID IN (:id2s))"); 
			tmp.put("id1s", orgAcl);
			tmp.put("id2s", orgAcl);
		} 
		List<TB_ORG_PARTY_STRUCTURE> list_gx = (List<TB_ORG_PARTY_STRUCTURE>) getResultList(hql.toString(),tmp);
		int j=1;
		for(TB_ORG_PARTY_STRUCTURE item : list_gx){
		   
			Map  node_c = tmpCache_jg.get(item.getCHILD_ID());
			Map  node_p = tmpCache_jg.get(item.getPARENT_ID()); 
			
			if(node_c!=null && node_p!=null){
				node_c.put("PID", node_p.get("ID"));
				if("TB_ORG_PERSON".equals(node_c.get("ENTITY_NAME"))){
					Map t_node_c = new HashMap();
					for(Object key : node_c.keySet()){
						t_node_c.put(key,node_c.get(key));
					}
				//	t_node_c.putAll(node_c); 
					node_c = t_node_c;
				}else{
					root.get("children").remove(node_c); 
				} 
				if(node_p.get("children")==null){
					node_p.put("children", new ArrayList<Map>() );
				}
				((ArrayList)node_p.get("children")).add(node_c);
				
			}
		}

		return root;
	}
	
	
	 
	
 
}
